package appBord

import grails.converters.JSON
import org.codehaus.groovy.grails.web.json.JSONObject

class AppBordGroupController {

    def index() {
        render AppBordGroup.list() as JSON
    }

    def initData(){
        AppBordGroup appBordGroup = AppBordGroup.newInstance()
        appBordGroup.groupName = "测试"
        appBordGroup.setGroupNo("ddddd")
        appBordGroup.save(flush: true)
    }
    def show(){
        def groupInstance
        if(params?.id){
            groupInstance = AppBordGroup.get(params?.id)
        }else{
            groupInstance = AppBordGroup.newInstance()
        }
        render(view: "/appBord/appBordGroup/update.gsp",model: [groupInstance:groupInstance])
    }

    def update(){

        def resultJson = JSONObject.newInstance()
        resultJson.put("statusCode",200)
        resultJson.put("message","操作成功")
        resultJson.put("closeCurrent",true)

        try {
            def groupNo = params.groupNo
            def groupInstance = AppBordGroup.findByGroupNo(groupNo)
            if(!groupInstance){
                groupInstance = AppBordGroup.newInstance()
            }

            def groupName = params.groupName

            groupInstance.groupNo = groupNo
            groupInstance.groupName = groupName
            groupInstance.save(flush: true)
        }catch (Exception e){
            resultJson.put("statusCode",-200)
            resultJson.put("message","Error:${e.getMessage()}")
            e.printStackTrace()
        }finally{
            render resultJson.toString()
        }
    }

    def delete(){
        def resultJson = JSONObject.newInstance()
        resultJson.put("statusCode",200)
        resultJson.put("message","操作成功")

        try {
            def  appBordGroup = AppBordGroup.get(params?.id)
            appBordGroup.delete(flush: true)
        }catch (Exception e){
            resultJson.put("statusCode",-200)
            resultJson.put("message","Error:${e.getMessage()}")
            e.printStackTrace()
        }finally{
            render resultJson.toString()
        }
    }
}
